package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.GuanlichuMapper;
import com.it.tydic.estate.module.mapper.inner.TbWxglDjbMapper;
import com.it.tydic.estate.module.model.inner.Guanlichu;
import com.it.tydic.estate.module.model.inner.TbWxglDjb;
import com.it.tydic.estate.module.service.WxglDjbService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 维修管理 实现
 * Created by jinghongtao on 2020/3/3.
 */
@Transactional
@Service
public class WxglDjbServiceImpl implements WxglDjbService{

    @Resource
    private TbWxglDjbMapper tbWxglDjbMapper;
    @Resource
    private GuanlichuMapper guanlichuMapper;
    @Resource
    private ProperteisUrls properteisUrls;
    /****
     * 分页查询 数据列表
     * @param record
     * @return
     */
    @Override
    public Result queryByPage(Map<String,Object> record) {
        Map<String,Object> map = new HashMap<>();
        CommonUtils.pageProcess(record);//验证处理分页
        record.put("zfbj","0");//查未删的数据
        Page<TbWxglDjb> djbList = tbWxglDjbMapper.selectLists(record);
        List<TbWxglDjb> result = djbList.getResult();
        if(result.size()>0){
            for (TbWxglDjb wxglDjb : result) {
                wxglDjb.setKhlbName(DictionaryCache.getDicVal(wxglDjb.getKhlb()));//客户类别
                wxglDjb.setBdfsName(DictionaryCache.getDicVal( wxglDjb.getBdfs()));//报单方式名称
                wxglDjb.setWxlbName(DictionaryCache.getDicVal(wxglDjb.getWxlb()));//维修类别
                wxglDjb.setWxdwName(DictionaryCache.getDicVal(wxglDjb.getWxdw()));//维修单位
                wxglDjb.setKhmydName(DictionaryCache.getDicVal(wxglDjb.getKhmyd()));//客户满意度wx_khmyd
            }
        }
        map.put("result",result);
        map.put("totle",djbList.getTotal());
        return Result.success(map);
    }
    /****
     * 将查询出的回访数据 Excel导出
     * @param record
     * @return
     */
    @Override
    public void excelExport(TbWxglDjb record, HttpServletResponse response)throws Exception {
        Integer pageNum = record.getPageNum();Integer pageSize = record.getPageSize();
        if(0==record.getPageNum() ){//如为0，则设默认值
            pageNum = 1;
        }
        if(0==record.getPageSize() ){
            pageSize = 20;
        }
        PageHelper.startPage(pageNum, pageSize);
        record.setZfbj("0");//查未删的数据
        Page<TbWxglDjb> wxglList = tbWxglDjbMapper.selectList(record);
        List<TbWxglDjb> result = wxglList.getResult();
        if(0<result.size()){
            for (TbWxglDjb wxglDjb : result) {
                wxglDjb.setBdfsName(DictionaryCache.getDicVal( wxglDjb.getBdfs()));//报单方式名称
                wxglDjb.setKhlbName(DictionaryCache.getDicVal(wxglDjb.getKhlb()));//客户类别
                wxglDjb.setWxdwName(DictionaryCache.getDicVal(wxglDjb.getWxdw()));//维修单位
                wxglDjb.setWxlbName(DictionaryCache.getDicVal(wxglDjb.getWxlb()));//维修类别
                wxglDjb.setKhmydName(DictionaryCache.getDicVal(wxglDjb.getKhmyd()));//客户满意度wx_khmyd

                if("0".equals(wxglDjb.getPg())){
                    wxglDjb.setPg("✘");
                }else {
                    wxglDjb.setPg("✔");
                }
                if("0".equals(wxglDjb.getWg())){
                    wxglDjb.setWg("✘");
                }else {
                    wxglDjb.setWg("✔");
                }
                if("0".equals(wxglDjb.getHf())){
                    wxglDjb.setHf("✘");
                }else {
                    wxglDjb.setHf("✔");
                }
            }
        }
        String excelType = record.getExcelType();//excel类型：2003 或 2003以上
        String excelName = "报单情况";
        String excelURL = properteisUrls.getExcelUrl();//Excel模板存放位置
        WxglExcel.createHfglExcel(result, response, excelName,excelType,excelURL);
    }
    /****
     * 维修数量统计 导出
     * @param record
     * @return
     */
    @Override
    public void numExport(Map<String,Object> record, HttpServletResponse response)throws Exception  {
        CommonUtils.pageProcess(record);//验证处理分页
        record.put("zfbj","0");//查未删的数据
        List<TbWxglDjb> wxtjResult = new ArrayList<>();
        if( "".equals(record.get("glc"))||null==record.get("glc") ){//判断是否有管理处
            //String glcid = record.get("glc").toString();
            List<Guanlichu> glcList = guanlichuMapper.selectAll();
            if(0<glcList.size()){
                for(Guanlichu guan:glcList){
                    String glc = guan.getId();
                    record.put("glc",glc);
                    Page<TbWxglDjb> wxtj = tbWxglDjbMapper.selectCountWx(record);
                    List<TbWxglDjb> wxtjList = wxtj.getResult();
                    if(0<wxtjList.size()){
                        wxtjResult.add(wxtjList.get(0));
                    }
                }
            }
        }else{
            Page<TbWxglDjb> xiaoquguanlis = tbWxglDjbMapper.selectCountWx(record);
            wxtjResult = xiaoquguanlis.getResult();
        }
        String excelType ="";
        if(!"".equals(record.get("excelType")) && null!=record.get("excelType") ){
            excelType = record.get("excelType").toString();//excel类型：2003 或 2003以上
        }
        String excelName = "维修数量统计";
        String excelURL = properteisUrls.getExcelUrl();//Excel模板存放位置
        WxglExcel.createWxtjExcel(wxtjResult, response, excelName,excelType,excelURL);

    }
    /****
     * 根据条件 查询维修数量统计
     * @param record
     * @return
     */
    @Override
    public Result selectCountWx(Map<String,Object> record) {
        Map<String,Object> map = new HashMap<>();
        CommonUtils.pageProcess(record);//验证处理分页
        record.put("zfbj","0");//查未删的数据
        if( "".equals(record.get("glc"))||null==record.get("glc") ){//判断是否有管理处
            //String glcid = record.get("glc").toString();
            List<Guanlichu> glcList = guanlichuMapper.selectAll();
            List<TbWxglDjb> wxtjResult = new ArrayList<>();
            if(glcList.size()>0){
                for(Guanlichu guan:glcList){
                    String glc = guan.getId();
                    record.put("glc",glc);
                    Page<TbWxglDjb> wxtj = tbWxglDjbMapper.selectCountWx(record);
                    List<TbWxglDjb> wxtjList = wxtj.getResult();
                    if(wxtjList.size()>0){
                        wxtjResult.add(wxtjList.get(0));
                    }
                }
            }
            map.put("result",wxtjResult);
            map.put("totle",wxtjResult.size());
            return Result.success(map);
        }
        Page<TbWxglDjb> xiaoquguanlis = tbWxglDjbMapper.selectCountWx(record);
        List<TbWxglDjb> result = xiaoquguanlis.getResult();
        map.put("result",result);
        map.put("totle",xiaoquguanlis.getTotal());
        return Result.success(map);
    }
    /**
     * 根据id 查询单条记录
     * @param id
     * @return
     */
    @Override
    public  Result selectByPrimaryKey(String id) {
        //checkInUtils.paramStringCheck(oid);//验证传入的参数不为空
        if("".equals(id) || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            TbWxglDjb wxglDjb = tbWxglDjbMapper.selectByPrimaryKey(id);
            wxglDjb.setBdfsName(DictionaryCache.getDicVal( wxglDjb.getBdfs()));//报单方式名称
            wxglDjb.setKhmydName(DictionaryCache.getDicVal(wxglDjb.getKhmyd()));//客户满意度wx_khmyd
            wxglDjb.setKhlbName(DictionaryCache.getDicVal(wxglDjb.getKhlb()));//客户类别
            wxglDjb.setWxlbName(DictionaryCache.getDicVal(wxglDjb.getWxlb()));//维修类别
            wxglDjb.setWxdwName(DictionaryCache.getDicVal(wxglDjb.getWxdw()));//维修单位
            return Result.success(wxglDjb);
        }

    }
    /**
     * 根据id 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id){
        if("".equals(id) || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            int i = tbWxglDjbMapper.deleteByPrimaryKey(id);
            if(1 == i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }

    }
    /**
     * 根据id 逻辑删除
     * @param id
     * @return
     */
    @Override
    public  Result deleteUp(String id){
        if("".equals(id) || id.length()==0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            int i = tbWxglDjbMapper.deleteUp(id);
            if(1 == i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }
    }
    /**
     * 根据id 作废
     * @param id
     * @return
     */
    @Override
    public  Result cancellationData(String id){
        int i = tbWxglDjbMapper.deleteUpdateById(id);
        if(1 == i){
            return   Result.success(CodeMsg.SUCCESS);
        }else {
            return   Result.error(CodeMsg.INTER_ERROR);
        }
    }
    /**
     * 根据id 恢复
     * @param id
     * @return
     */
    @Override
    public  Result recoverData(String id){
        int i = tbWxglDjbMapper.recoverData(id);
        if(1 == i){
            return   Result.success(CodeMsg.SUCCESS);
        }else {
            return   Result.error(CodeMsg.INTER_ERROR);
        }
    }
    /**
     *  批量逻辑删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(String[] ids){
        if (ids.length == 0) {
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }
        int result = tbWxglDjbMapper.deleteByList(ids);
        if (result == ids.length) {
            return  Result.success(CodeMsg.SUCCESS);
        }
        return Result.error(CodeMsg.DELETE_ERROR, null);
    }
    /**
     * 根据id 更新
     * @param record
     * @return
     */
    @Override
    public  Result updateData(TbWxglDjb record){
        //record.setBdczsj(new Date());//报单操作时间
        //record.setBdczry("");//报单操作人员
        if(null==record.getId()||"".equals(record.getId())){
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }else {
            if(null==record.getLiucheng()||"".equals(record.getLiucheng())){
                if("1".equals(record.getLiucheng()) ){
                    record.setPgczsj(new Date());//派工操作时间
                }else if("2".equals(record.getLiucheng()) ){
                    record.setWgczsj(new Date());//完工操作时间
                }else if("3".equals(record.getLiucheng()) ){
                    record.setHfczsj(new Date());//回访操作时间
                }
            }
            record.setGxsj(new Date());//更新时间
            record.setGxr(record.getBdczry());//更新人
            int i = tbWxglDjbMapper.updateByPrimaryKeySelective(record);
            if(1 == i){
                return  Result.success(CodeMsg.SUCCESS);
            }else {
                return Result.error(CodeMsg.UPDATE_ERROR,null);
            }
        }

    }
    /**
     * 保存
     * @param record
     * @return
     */
    @Override
    public Result saveDate(TbWxglDjb record){
        if(null == (record.getId()) || "".equals(record.getId())){
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());
            record.setId(String.valueOf(id));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String bdlsh = sdf.format(new Date());
            record.setBdlsh(bdlsh);//报单流水号
            record.setBdczsj(new Date());//报单操作时间
            record.setGxsj(new Date());//更新时间
            record.setGxr(record.getBdczry());
            //record.setBdczry("");//报单操作人员
            record.setZfbj("0");//删除标记：0正常，1 删除
            record.setYxzt("1");//有效状态：0作废，1 有效
            record.setPg("0");//派工：0否，1是
            record.setWg("0");//完工：0否，1是
            record.setHf("0");//回访：0否，1是
            int i = tbWxglDjbMapper.insertSelective(record);
            if(1 == i){
                return  Result.success(record);
            }else {
                return  Result.error(CodeMsg.INSERT_ERROR,null);
            }
        }else {
            return  Result.error(CodeMsg.INSERT_ERROR,null);
        }

    }

}
